package com.czm.webapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

@SuppressLint("SetJavaScriptEnabled")
public class WebViewActivity extends Activity {
    private WebView mWeb;
    private Intent mIntent;
    private ProgressBar mWebViewProgressBar;
    private String sUrl = "http://www.haitaolab.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        mWebViewProgressBar = (ProgressBar) findViewById(R.id.mWebViewProgressBar);
        //设置内容
        mWeb = (WebView) findViewById(R.id.webMain);
        WebSettings webSetting = mWeb.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setSupportZoom(false);
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSetting.setAllowFileAccess(true);
        mWeb.setWebViewClient(new MyWebViewClient());
        mWeb.setWebChromeClient(new MyWebChromeClient());
        //获取参数，加载页面
        mWeb.loadUrl(sUrl);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWeb.canGoBack()) {
            mWeb.goBack();
            return true;
        } else {
            super.onKeyDown(keyCode, event);
            return true;
        }
    }

    private class MyWebViewClient extends WebViewClient {
    }

    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
        }

        //覆盖默认的window.alert
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            return true;
        }

        //覆盖默认的window.confirm
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            return true;
        }

        //取得浏览页面的标题
        public void onReceivedTitle(WebView view, String title) {
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                mWebViewProgressBar.setVisibility(View.GONE);
            } else {
                if (mWebViewProgressBar.getVisibility() == View.GONE)
                    mWebViewProgressBar.setVisibility(View.VISIBLE);
                mWebViewProgressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }
}
